package controllers

import (
	"app/app/services"
	"app/models"
	"app/unit"

	"github.com/kataras/iris/v12"
)

type LoginController struct {
	Ctx iris.Context
}

func (c *LoginController) Post() *models.JsonResult {
	userName := c.Ctx.PostValueTrim("account")
	password := c.Ctx.PostValueTrim("password")
	if userName == "" || password == "" {
		return models.JsonErrorCode(400, "账号密码不可为空")
	}
	user := services.UserService.FindByAccount(userName)
	if unit.MD5(password) != user.Password {
		return models.JsonErrorCode(401, "密码错误")
	}
	SetUser(int(user.Id), user.Roleid, user.Name, c.Ctx)
	return models.JsonData(user)
}

func (c *LoginController) Get() *models.JsonResult {
	Clean(c.Ctx)
	return models.JsonData("退出")
}
